package com.naodong.shenluntiku.intergation.jobqueue;

import android.content.Context;
import android.util.Log;

import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.config.Configuration;
import com.birbit.android.jobqueue.log.CustomLogger;

/**
 * Created by grey on 2017/6/15.
 */

public class BackgroundManage {

    private static BackgroundManage instance;

    private JobManager jobManager;

    public static BackgroundManage getInstance() {
        if (instance == null) {
            instance = new BackgroundManage();
        }
        return instance;
    }

    public synchronized JobManager getJobManager() {
        return jobManager;
    }

    public void initJobManager(Context context) {
        Configuration.Builder builder = new Configuration.Builder(context)
                .customLogger(new CustomLogger() {
                    private static final String TAG = "shenluntikuapp job";

                    @Override
                    public boolean isDebugEnabled() {
                        return true;
                    }

                    @Override
                    public void d(String text, Object... args) {
                        Log.d(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(Throwable t, String text, Object... args) {
                        Log.e(TAG, String.format(text, args), t);
                    }

                    @Override
                    public void e(String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }

                    @Override
                    public void v(String text, Object... args) {

                    }
                })
                .minConsumerCount(1)//最少存活1个空闲线程
                .maxConsumerCount(3)//最多存活3个线程
                .loadFactor(3)//
                .consumerKeepAlive(120);//保存持续时间
        jobManager = new JobManager(builder.build());
    }

}
